﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Xml.XPath;

namespace SmartSpace.WebApp._console
{
    public partial class _console : System.Web.UI.MasterPage
    {
        private string _curMenuId;
        private string _curSubMenuId;

        XElement _curMenu;

        protected void Page_Load(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            _ctlLinkUser.InnerText = service.GetUser(SsSite.Current.SuperAdminUserId.Value).Name;

            _ctlLabelDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

            List<XMenu> menus = new List<XMenu>();

            XDocument xDoc = XDocument.Load(Server.MapPath("~/_console/menu.config"));
            XElement menu = xDoc.Element("menu");

            string curUrl = "~" + Request.Url.AbsolutePath;

            XElement xMenu = xDoc.XPathSelectElement("//menu[@url='" + curUrl + "']");

            if (xMenu != null)
            {
                if (xMenu.Parent == xDoc.Root)
                {
                    _curMenu = xMenu;
                    _curMenuId = xMenu.Attribute("id").Value;
                }
                else
                {
                    _curMenu = xMenu.Parent;
                    _curMenuId = xMenu.Parent.Attribute("id").Value;
                    _curSubMenuId = xMenu.Attribute("id").Value;
                }
            }


            StringBuilder htmlMenu = new StringBuilder();
            foreach (var item in menu.Elements("menu"))
            {
                string id = item.Attribute("id").Value;
                string caption = item.Attribute("caption").Value;
                string url = item.Attribute("url").Value;

                url = url.Replace("./", "~/_console/");
                url = Page.ResolveUrl(url);

                string icon = string.Empty;
                if (item.Attribute("icon") != null)
                {
                    icon = item.Attribute("icon").Value;
                    icon = Page.ResolveUrl(icon);
                }
                else
                {
                    icon = Page.ResolveUrl("~/_console/resources/images/opened.png");
                }
                icon = Page.ResolveUrl("~/App_themes/adminica/images/laptop.png");

                if (item.HasElements)
                {
                    htmlMenu.AppendFormat("<li class='has_drawer'><a href='{0}'><img src='{1}'/><span>{2}</span></a>", "#", icon, caption);
                    htmlMenu.Append("<ul class='drawer'>");
                    foreach (var subItem in item.Elements("menu"))
                    {
                        string subId = subItem.Attribute("id").Value;
                        string subCaption = subItem.Attribute("caption").Value;
                        string subUrl = subItem.Attribute("url").Value;
                        subUrl = Page.ResolveUrl(subUrl);

                        string subIcon = string.Empty;
                        if (subItem.Attribute("icon") != null)
                        {
                            subIcon = subItem.Attribute("icon").Value;
                            subIcon = Page.ResolveUrl(subIcon);
                        }
                        else
                        {
                            subIcon = Page.ResolveUrl("~/_console/resources/images/opened.png");
                        }
                        subIcon = Page.ResolveUrl("~/App_themes/adminica/images/laptop.png");
                        htmlMenu.AppendFormat("<li><a href='{0}'><img src='{1}'/><span>{2}</span></a></li>", subUrl, subIcon, subCaption);
                    }
                    htmlMenu.Append("</ul>");
                    htmlMenu.Append("</li>");
                }
                else if (_curMenuId == id)
                {
                    htmlMenu.AppendFormat("<li class='current'><a href='{0}'><img src='{1}'/><span>{2}</span></a></li>", url, icon, caption);
                }
                else
                {
                    htmlMenu.AppendFormat("<li><a href='{0}'><img src='{1}'/><span>{2}</span></a></li>", url, icon, caption);
                }

            }

            _ctlListMenu.Mode = LiteralMode.Transform;
            _ctlListMenu.Text = htmlMenu.ToString();

            //List<XMenu> subMenus = new List<XMenu>();
            //if (_curMenu != null)
            //{
            //    foreach (var item in _curMenu.Elements("menu"))
            //    {
            //        string id = item.Attribute("id").Value;
            //        string caption = item.Attribute("caption").Value;
            //        string url = item.Attribute("url").Value;

            //        url = url.Replace("./", "~/_console/");

            //        string icon = string.Empty;
            //        if (item.Attribute("icon") != null)
            //        {
            //            icon = item.Attribute("icon").Value;
            //        }
            //        subMenus.Add(new XMenu { Id = id, Caption = caption, Url = url, IconUrl = icon });
            //    }
            //}
            //_ctlListMenu.DataSource = menus;
            //_ctlListMenu.DataBind();

            //_ctlListSubMenu.DataSource = subMenus;
            //_ctlListSubMenu.DataBind();
        }

        public string IsActived(object id)
        {
            if (id.ToString() == _curMenuId)
            {
                return "active";
            }
            return "menu";
        }

        public string IsSubActived(object id)
        {
            if (id.ToString() == _curSubMenuId)
            {
                return "active";
            }
            return "menu";
        }


        public bool Compare(Uri url1, Uri url2)
        {
            if (url1.Segments[url1.Segments.Length - 1] == url2.Segments[url2.Segments.Length - 1])
            {
                return true;
            }
            return false;
        }
    }

    public class XMenu
    {
        public string Id { get; set; }
        public string Caption { get; set; }
        public string Url { get; set; }
        public string IconUrl { get; set; }
    }
}